package buildrobot_info

import (
	"go_server/game_server/api/service/bag"
	"go_server/game_server/api/service/item"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
	"math"
)

func (s *Buildrobot_info_service) S2C_BuildRobotChargeByItem(conn ziface.IConnection, roleid int64, msg *pb.BuildRobotChargeByItemC2S) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	datas, code := s.onBuildRobotChargeByItem(robot, msg.Charges)
	if datas != nil {
		msgS2C := &pb.BuildRobotChargeByItemS2C{}
		msgS2C.Robots = datas
		if msgS2C != nil {
			zlog.Debugf("ProtocolManager_BuildRobotChargeByItem %v", msgS2C)
			defer func() {
				conn.SendBuffPBMsg(uint32(pb.ProtocolManager_BuildRobotChargeByItem), msgS2C)
			}()
		}
	}
	return code
}

func (s *Buildrobot_info_service) onBuildRobotChargeByItem(robot *Cache_buildrobot_info, brcs []*pb.BuildRobotChargeByItemC2S_BRC) ([]*pb.BuildRobotData, pb.ErrorCodeManager) {
	if robot != nil && brcs != nil && len(brcs) > 0 {
		charges := util.NewSet[int32]()
		charges.AddValue(item.GetRobotStrengthItems())

		useitem := make(map[int32]int32)  //物品id
		addpower := make(map[int32]int32) //RobotSerid:机器人id

		isAllMaxPower := true
		for _, brv := range brcs {
			for id, count := range brv.Items {
				if charges.Contains(id) { //合法的物品
					if im, i1 := config_manager.GetTableItemSheetItemConfig(id); i1 {
						if rv, ok := robot.GetRobotBySerid(brv.RobotSerid); ok {
							if cfg := config_manager.GetTableRobotSheetRobot(rv.Cfgid); cfg != nil {
								isAllMaxPower = isAllMaxPower && (rv.Power == cfg.PowerRange[1]) //是否体力最大值
								if rv.Power != cfg.PowerRange[1] {                               //体力最大值
									if _, p1 := addpower[rv.Serid]; !p1 {
										addpower[rv.Serid] = 0
									}

									if _, p1 := useitem[id]; !p1 {
										useitem[id] = 0
									}

									for i := 0; i < int(count); i++ { //道具添加的体力
										addpower[rv.Serid] += im.Par[1]
									}
									useitem[id] += count //添加使用的道具
								}
							}
						}
					}
				}
			}
		}

		if isAllMaxPower {
			return nil, pb.ErrorCodeManager_BUILDROBOTINFO_ERROR_MAXPOWER
		} else {
			isEnough := bag.CheckBatchItemEnough(robot.RoleId, useitem)
			if isEnough {
				rstRobots := make([]*pb.BuildRobotData, 0)

				for rsid, pv := range addpower {
					if rv, r1 := robot.RobotData[rsid]; r1 {
						if cfg := config_manager.GetTableRobotSheetRobot(rv.Cfgid); cfg != nil {
							min := cfg.PowerRange[0] //体力最小值
							max := cfg.PowerRange[1] //体力最大值

							rv.Power += pv
							rv.Power = int32(math.Max(float64(rv.Power), float64(min)))
							rv.Power = int32(math.Min(float64(rv.Power), float64(max)))

							rstRobots = append(rstRobots, rv.To_PbData())
						}
					}
				}

				go bag.SubItemBatch(robot.RoleId, useitem)
				defer robot.Save_Only_RobotData()
				return rstRobots, pb.ErrorCodeManager_SUCCESS
			} else {
				return nil, pb.ErrorCodeManager_ITEM_NOT_ENOUGH
			}
		}
	}
	return nil, pb.ErrorCodeManager_PARAM_ERROR
}
